package com.invengo.module_check.ui.fragment.opeartion;

import static com.invengo.module_check.data.entity.BundleKey.KEY_ACTION;
import static com.invengo.module_check.data.entity.BundleKey.KEY_BOOKS;
import static com.invengo.module_check.data.entity.BundleKey.KEY_IS_SHELF_RESULT;
import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELFS;
import static com.invengo.module_check.data.entity.BundleKey.KEY_TITLE;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentOpeartionBinding;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;

public class OpeartionFragment extends BaseFragment<FragmentOpeartionBinding, OpeartionViewModel> {
    public String byBooksTip = "成功上架 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 本" +
            ",失败 <span style=\"color:#FF7E5A; font-weight:bold;\">%d</span> 本";
    public String downBookTipSuccess = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 本";
    public String downBookTipFail = "共 <span style=\"color:#FF7E5A; font-weight:bold;\">%d</span> 本";
    public String byShelf_Success = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 层";
    public String byShelf_Fail = "共 <span style=\"color:#FF7E5A; font-weight:bold;\">%d</span> 层";
    public String inventoryTip = "上架 <span style=\"color:#4883e7; font-weight:bold;\">%d</span> 本，" +
            "在架 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 本<br />" +
            " 外借 <span style=\"color:#FDA344; font-weight:bold;\">%d</span> 本，" +
            "错架 <span style=\"color:#FF7E5A; font-weight:bold;\">%d</span> 本";

    public String changeTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 本";
    public String forceTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 本";
    public String revokeTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 本";

    public String firstPos = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 层";
    List<BookBean> bookBeans;
    List<ShelfBean> shelfBeans;
    ActionArgs actionArgs;
    Map<String, List<BookBean>> collect;
    String currentShelf;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_opeartion;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        actionArgs = (ActionArgs) getArguments().getSerializable(KEY_ACTION);
        currentShelf = getArguments().getString(BundleKey.KEY_CURRENT_SHELF);
        bookBeans = (List<BookBean>) getArguments().getSerializable(KEY_BOOKS);
        shelfBeans = (List<ShelfBean>) getArguments().getSerializable(KEY_SHELFS);
        if (bookBeans==null)
            bookBeans =new ArrayList<>();
        if (shelfBeans==null)
            shelfBeans =new ArrayList<>();
        boolean state = getArguments().getBoolean(KEY_OP_STATE);
        switch (actionArgs) {
            case ACTION_LISTING:
                viewModel.showListing(state, currentShelf, "(请求失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(byBooksTip, state ? bookBeans.size() : 0, state ? 0 : bookBeans.size())));
                break;
            case ACTION_DOWN:
                boolean isSelfList = getArguments().getBoolean(KEY_IS_SHELF_RESULT);
                if (isSelfList) {
                    collect = StreamSupport.stream(bookBeans).collect(Collectors.groupingBy(BookBean::getShelfName));
                    viewModel.showDown(state, collect, "(请求失败)");
                    binding.bookResult.setText(Html.fromHtml(String.format(state ? byShelf_Success : byShelf_Fail, collect.keySet().size())));
                } else {
                    viewModel.showDown(state, "(请求失败)");
                    binding.bookResult.setText(Html.fromHtml(String.format(state ? downBookTipSuccess : downBookTipFail, bookBeans.size())));
                }
                break;
            case ACTION_INVENTORY:
                List<Integer> statistics = (List<Integer>) getArguments().getSerializable(BundleKey.KEY_INVENTORY_PARALLERS_COUNT);
                viewModel.showInventory(state, currentShelf, "(请求失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(inventoryTip, statistics.get(0), statistics.get(1), statistics.get(2), statistics.get(3))));
                break;
            case ACTION_CHANGE:
                viewModel.showCharge(state, currentShelf, "(请求失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(changeTip, bookBeans.size())));
                break;
            case ACTION_PARALLEL:
                viewModel.showParaller(state, currentShelf, "(请求失败)");
                binding.bookResult.setText("");
                break;
            case ACTION_FIRST:
                viewModel.showFirstPos(state, shelfBeans.get(0).getShelfName() + "-" + shelfBeans.get(shelfBeans.size() - 1).getShelfName(), "(请求失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(firstPos, shelfBeans.size())));
                break;
            case ACTION_FORCE:
                viewModel.showForce(state, currentShelf, "(请求失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(forceTip, bookBeans.size())));
                break;
            case ACTION_REVOKE:
                viewModel.showRevoke(state, "(请求失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(revokeTip, bookBeans.size())));
                break;
            case ACTION_FIRST_CHECK:
                viewModel.showFirstCheck(state, "(更新失败)");
                binding.bookResult.setText(Html.fromHtml(String.format(revokeTip, bookBeans.size())));
                break;
        }
    }

    @Override
    public void initViewObservable() {
        viewModel.ui.launchHomeEvent.observe(this, unused -> NavHostFragment.findNavController(this).popBackStack(actionArgs.getHomeAction(), false));
        viewModel.ui.launchWorkEvent.observe(this, unused -> NavHostFragment.findNavController(this).popBackStack(actionArgs.getWorkAction(), false));
        viewModel.ui.launchInfoEvent.observe(this, unused -> {
//            LocalLogUtil.temp2File("-----OpeartionFragment----------");
//            for (BookBean bookBean : bookBeans) {
//                LocalLogUtil.temp2File(bookBean.getBookName()+"  "+bookBean.getBarCode()+"  "+ bookBean.getTip()+"  外借状态： "+ bookBean.getState());
//            }
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_BOOKS_GROUP, (Serializable) collect);
            bundle.putSerializable(BundleKey.KEY_BOOKS, (Serializable) bookBeans);
            bundle.putSerializable(BundleKey.KEY_SHELFS, (Serializable) shelfBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, actionArgs);
            bundle.putString(BundleKey.KEY_CURRENT_SHELF, currentShelf);
            bundle.putBoolean(KEY_IS_SHELF_RESULT, getArguments().getBoolean(KEY_IS_SHELF_RESULT));
            bundle.putString(BundleKey.KEY_TITLE, getArguments().getString(KEY_TITLE) + "详情");
            NavHostFragment.findNavController(this).navigate(actionArgs.getResult2info(), bundle);
        });
    }
}
